import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
// 创建Home组件并在router/index.js中引进来sthh
import Home from '../views/Home'
import Welcome from '../views/Welcome.vue'
import User from '../views/User'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  // 编辑路由规则
  {
    path: '/home',
    component: Home,
    // 直接询问
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to:进入的那个路由
  // from:从哪里来
  // next：放行
  console.log(to)
  console.log(from)
  const token = sessionStorage.getItem('token')
  // token本地没有 undefined false
  // token 本地有值 'Bear' true
  if (token) {
    next()
  } else {
    if (to.path === '/login') return next()
    next('login')
  }
})

export default router
